package com.eascs.spider.kuaixiao.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongodbUtil {
	
	private static Logger logger = LoggerFactory.getLogger(MongodbUtil.class);
	private static MongoClient mongoClient = null;
	private static MongoDatabase mongoDatabase = null;
	
	//公司配置
  static{
	    Map<String,String> mgo = DBUtil.getProperties();
        ServerAddress serverAddress = new ServerAddress(
        		mgo.get("mongodb.ip"),Integer.valueOf(mgo.get("mongodb.port"))); 
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();  
        addrs.add(serverAddress);  
        MongoCredential credential = MongoCredential.createScramSha1Credential(
        		mgo.get("mongodb.user"), mgo.get("mongodb.library"), 
        		mgo.get("mongodb.password").toCharArray());  
        
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
        credentials.add(credential);  
        mongoClient = new MongoClient(addrs,credentials);  
        mongoDatabase = mongoClient.getDatabase(mgo.get("mongodb.library"));  
	}
	
	//家里配置
	/*static{
        mongoClient = new MongoClient("192.168.14.131" , 27017 );  
        mongoDatabase = mongoClient.getDatabase("spider");  
	}*/
	
	private static void set(String collectionName ,Map<String, Object> data){
		MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collectionName);
		Document document = new Document(data);
		mongoCollection.insertOne(document);
	}
	
	/**
	 * 检索指定集合中所有文档
	 * @param collectionName
	 */
	/*private static void get(String collectionName){
		MongoCollection<Document> mc = mongoDatabase.getCollection(collectionName);
		FindIterable<Document> findIterable = mc.find();  
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        Map<String, String> map = new HashMap<>();
        int count =0;
        while(mongoCursor.hasNext()){  
           Document document = mongoCursor.next();
           if(!map.containsKey(document.getString("ID"))){
        	   map.put(document.getString("ID"), "");
           }else{
        	   count++;
        	   System.out.println(document.getString("ID"));
           }
           
        }  
        System.out.println("map容量："+map.size());
        System.out.println("重复数据量:" + count);
	}*/
	
	public static void setShunqiwang(Map<String , Object> data){
		set("shunqiwang", data);
	}
	
	//51job条件查询
	/*public static List<Document> query51job(String id,String expAdress , String keyword , String ageMin ,String ageMax){
		MongoCollection<Document> collection = mongoDatabase.getCollection("51job");
		Document queryParam = new Document();
		if(!StringUtil.isNullOrEmpty(id))
			queryParam.append("ID", id);
		if(!StringUtil.isNullOrEmpty(expAdress))
			queryParam.append("期望工作地区", expAdress);
		if(!StringUtil.isNullOrEmpty(keyword))
			queryParam.append("关键词", keyword);
		if(!StringUtil.isNullOrEmpty(ageMax) && !StringUtil.isNullOrEmpty(ageMin))
			queryParam.append("年龄范围", ageMin+"-"+ageMax);
		
		FindIterable<Document> findIterable = collection.find(queryParam);
		MongoCursor<Document> mongoCursor = findIterable.iterator();  
		List<Document> list = new ArrayList<>();
        while(mongoCursor.hasNext()){  
           list.add(mongoCursor.next());  
        } 
        return list;
	}*/
	
	/*public static void update51job(String id ,String address){
		MongoCollection<Document> collection = mongoDatabase.getCollection("51job");
		collection.updateMany(Filters.eq("ID", id), new Document("$set",new Document("期望工作地区" ,address)));
	}*/
	
	public static void main(String[] args) {
		MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("shunqiwang");
		Map<String, String> map = new HashMap<>();
		map.put("1111", "2222222222");
		Document document = new Document("name" ,map);
		mongoCollection.insertOne(document);
       
	}
}
